 
var elRedTarget = null;
$(function(){
	 //Red
   
    $('input.red').bind("click", function (e) {
	    var jsondata = $(this).parent().find('.fvars').attr("value");
		//var jsondata = $(this).parent().find('.fvars').text();
		var val = eval("("+jsondata+")");
        doCallRED(val);
        elRedTarget = $(this);
    });
	
	 $('#chapter-marking .replace-vd').bind("click", function (e) {
        e.preventDefault();
		if(confirm('Replacing video will recall the existing one for production. Confirm replacing the video?'))
		{
			var jsondata = $(this).parent().find('.fvars').val();
			var val = eval("("+jsondata+")");
			callBLUE(val);
		}
    });
	
	var bg = document.createElement("div");
	bg.id = "mask";
	$(bg).css({position:"absolute",zIndex:10,backgroundColor:'#000',opacity: 0.3}).hide();
	$("body").prepend(bg);
	
	
})
function showmask(w,h){

	$("#mask").css({top:"0px",left:"0px",width:$(window).width()+"px",height:$(document).height()+"px"});

	var box = $("#popupCont");
	var t = $(window).height()/2-h/2+$(window).scrollTop()+"px" || $(window).height()/2-$(box).height()/2+$(window).scrollTop()+"px";
	var l= $(window).width()/2-w/2+"px" || $(window).width()/2-$(box).width()/2+"px";
	
	box.css({top:t,left:l,zIndex:11,position:'absolute'}).show();

	
	$(window).resize(function(){		
		this.resetwin();
		
		$("#mask").css({width:$(window).width(),height:$(document).height()});
	})
	$(window).scroll( function() {
		this.resetwin();
				   
	});
	
	this.resetwin = function()
	{
		box.css({top:($(window).height()/2-$(box).height()/2+$(window).scrollTop())+"px",left:$(window).width()/2-$(box).width()/2+"px"});	
	}
	
}


function close_div(){
	$("#mask").hide();
	$("#myContent").hide();
	$("#popupCont").hide();

}

function callSave(val){
	
	var tmplast = parseInt($(elRedTarget).parent().find("strong:last").text());
	var newselect = val - tmplast;
    var container;
	$(elRedTarget).parent().find("strong:first").text(newselect);
	$(elRedTarget).parent().find("strong:last").text(val);

	close_div();

    if (TVB.util.isSet('TVB.Pdes.Mod.getFolderItems')) {
        container = $(elRedTarget).parent().find('div.item-list').get(0);
        TVB.Pdes.Mod.getFolderItems(container);
    }

}
function callRED(obj) {
	var flashvars = obj;
	var params = {
	allowFullScreen: "true",
	allowScriptAccess: "always"//,
	//wmode: "opaque"
	};
	var attr = {
	id: "myContent",
	name: "myContent"
	};	
	swfobject.embedSWF("swf/red.swf?ver=20090721", "myContent", "649", "441", "9.0.0", "swf/playerProductInstall.swf", flashvars, params, attr);

	
}
function callBLUE(obj) {
	showmask(526,310);
	$("#mask").show();
	$("#myContent").show();
	obj.caller = 'html';
	var flashvars = obj;
	var params = {
	allowFullScreen: "true",
	allowScriptAccess: "always"//,
	//wmode: "opaque"
	};
	var attr = {
	id: "myContent",
	name: "myContent"
	};	
	swfobject.embedSWF("swf/blue.swf?ver=20090721", "myContent", "526", "310", "9.0.0", "swf/playerProductInstall.swf", flashvars, params, attr);

	
}

function closeBlue(bolModify){
	if(bolModify)
	{
		$("#player").html('<embed height="208" width="336" type="application/x-shockwave-flash" src="swf/mediaplayer.swf" id="mpl" name="mpl" quality="high" allowscriptaccess="always" allowfullscreen="true" wmode="opaque" flashvars="height=208&amp;width=336&amp;file=http://pdes.tvb.com/swf/src/assets/process.png&amp;id=http://pdes.tvb.com/swf/src/assets/process.png&amp;overstretch=fit&amp;shuffle=false&amp;enablejs=true&amp;image=http://pdes.tvb.com/swf/src/assets/process.png"/>')
		
	}
	close_div();
	location.reload();
	
}

function doCallRED(param){	
	callRED(param);
	$("#mask").show();
	$("#myContent").show();
	showmask(649,441);
	
}
var oPlayer = null;
TVB.RED = {	
	
	callHTMLPlayer:function(obj)
	{
		
		var title = obj.title;
		var src = obj.src;
		var img = obj.img;
		
		function sendEvent(swf,typ,prm){
            thisMovie(swf).sendEvent(typ,prm);
        }
        function thisMovie(swf) {
            if(navigator.appName.indexOf("Microsoft") != -1) {return window[swf];}
            else return document[swf];
        }
        function loadFile(swf,obj) {
            var movie = thisMovie(swf);
            movie.loadFile(obj);
            //thisMovie(swf).loadFile(obj);
        }
        function loadVideo(theFile, theThumb) {
            if (theThumb) loadFile('mpl',{"file":theFile,"image":theThumb});
            else loadFile('mpl',{file:theFile});
            sendEvent('mpl','playpause');
        }
        function initPlayer(url, file, thumb) {
            var width = "420";
            var height= "260";
            var so = new SWFObject('swf/mediaplayer.swf','mpl',width,height,'8');
            so.addParam('allowscriptaccess','always');
            so.addParam('allowfullscreen','true');
            so.addParam('wmode','opaque');
            so.addVariable('height',height);
            so.addVariable('width',width);
            so.addVariable('overstretch','fit');
            so.addVariable('shuffle','false');
            so.addVariable('enablejs','true');
            so.addVariable("autostart","true");
            so.addVariable('searchbar', 'false');
            so.addVariable("file", url);
            so.addVariable('id', file);
            so.addVariable("image", thumb);
            so.write('player');
        }

       
		
		
			$("#myContent").css('visibility','hidden');

			 if (oPlayer === null) {
				oPlayer = $("#pop-player").dialog({
					bgiframe: true,
					resizable: false,
					width: 450,
					autoOpen: false,
					modal: true,
					beforeclose: function () {
						
						sendEvent('mpl','playpause');

						
					},
				});
			 }
			arrSrc = src.split('*');
			if (arrSrc.length === 1)
			{
				arrSrc[1] = '';
			}
			oPlayer.dialog('option', 'title', title);
			oPlayer.dialog('open');
			oPlayer.bind('dialogclose', function(event, ui) {			
				$("#myContent").css('visibility','visible');			
			});


			initPlayer(arrSrc[0], arrSrc[1], img);

	}
}


